﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class Main_Utilities_State : System.Web.UI.Page
{
    string sql = "";
    Datautility du = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
           if (Session["userid"] == null)
        {
            //ScriptManager.RegisterStartupScript(Page, this.GetType(), "redirect", "window.open('../../login.aspx','_parent');", true);
            Response.Redirect("~/SessionExpired.aspx");
        }
          if (Request.QueryString["action"] != null)
          {
              if (Request.QueryString["action"].ToString() == "addnew")
              {
                  PnlAddNew.Visible = true;
                  if (!IsPostBack)
                  {
                      BindDDlCountry();
                  }
              }
              else if (Request.QueryString["action"].ToString() == "view")
              {
                  if (!IsPostBack)
                  {
                      PnlView.Visible = true;
                      BindGridState();
                  }
              }
          }
    }
    protected void BindDDlCountry()
    {
        sql = "select CountryId, CountryName from mas_country";
        DataTable dt = du.getdata(sql);
        DdlCountry.DataSource = dt;
        DdlCountry.DataTextField = "CountryName";
        DdlCountry.DataValueField = "CountryId";
        DdlCountry.DataBind();        
    }
    protected void BindGridState()
    {
        DataTable dt = du.getdatausingprocedure("usp_mas_Getstate");
        GrdState.DataSource = dt;
        GrdState.DataBind();
    }
    protected void SaveState()
    {
        SqlParameter[] parmstate = { new SqlParameter("@Countryid",DdlCountry.SelectedValue),
                                       new SqlParameter("@statename",TxtState.Text.Trim()),
                                       new SqlParameter("@statecode",TxtStateCode.Text.Trim() )
                                   };
        du.getdatausingprocedure("usp_mas_saveState", parmstate);
    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        //sql = "select Stateid, StateCode, CountryId, StateName from mas_state ";
        //DataTable dt = du.getdata(sql);
        //if (dt.Rows.Count > 0)
        //{
        //    if (dt.Rows[0]["StateName"].ToString() == TxtState.Text)
        //    {
                SaveState();
                Response.Redirect("State.aspx?action=view");
        //    }
        //    else
        //    {
        //        lblMsg.Attributes.Add("class", "false");
        //        lblMsg.Text = "Name Already Exists!";
        //    }
        //}
    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {

    }
    protected void GrdState_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GrdState.EditIndex = -1;
        BindGridState();
    }
    protected void GrdState_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GrdState.EditIndex = e.NewEditIndex;
        BindGridState();
    }
    protected void GrdState_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            string key = GrdState.DataKeys[e.RowIndex].Value.ToString();
            TextBox statename = (TextBox)GrdState.Rows[e.RowIndex].Cells[1].Controls[0];
            DropDownList ddlCountry = (DropDownList)GrdState.Rows[e.RowIndex].Cells[2].Controls[1];
            if (statename.Text != string.Empty)
            {
                SqlParameter[] parmstate = { new SqlParameter("@Stateid", key),
                                       new SqlParameter("@countryid", ddlCountry.SelectedValue),
                                       new SqlParameter("@Statename", statename.Text.Trim())
                                   };
                du.executeprocedure("usp_mas_UpdateState", parmstate);
                GrdState.EditIndex = -1;
                BindGridState();
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = " updated successfully...";
            }
            else
            {
                lblMsg.Text = "State name cannot be empty!";
                lblMsg.Attributes.Add("class", "False");
            }
        }
        catch (Exception ex)
        {

        }
    }
    protected void GrdState_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex == GrdState.EditIndex )
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlCountryname");
                HiddenField hf = (HiddenField)e.Row.FindControl("HFPrimary");
                HiddenField hf1 = (HiddenField)e.Row.FindControl("HiddenField1");
                sql = "select CountryId, CountryName from mas_country";
                DataTable dt = du.getdata(sql);
                ddl.DataSource = dt;
                ddl.DataTextField = "CountryName";
                ddl.DataValueField = "CountryId";
                ddl.DataBind();
                if (hf.Value != string.Empty)
                {
                    ddl.SelectedValue = hf.Value;
                }
            }
        }
    }
    protected void GrdState_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GrdState.PageIndex = e.NewPageIndex;
        BindGridState();
    }
}
